const db = require("../config/mysql_util");
class GoodsModel {
  constructor() {}
   findById(goods_id) {
    return db.query("select * from goods where goods_id = ?", [goods_id]);
  }
  getCount(data) {
    let sql = ` select count(*) count from goods where 1 `;
    if (data.goods_name) sql += ` and goods_name like '%${data.goods_name}%' `;
    if (data.cat_one_id) sql += ` and cat_one_id like '%${data.cat_one_id}%' `;
    if (data.goods_state) sql += ` and goods_state = ${data.goods_state} `;
    if (data.delete_time) sql += ` and delete_time = ${data.delete_time} `;
    return db.query(sql);
  }
  findGoodsList(data) {
    let limit = data.pagesize * (data.page - 1);
    let sql = ` select * from goods where 1 `;
    if (data.goods_name) sql += ` and goods_name like '%${data.goods_name}%' `;
    if (data.cat_one_id) sql += ` and cat_one_id like '%${data.cat_one_id}%' `;
    if (data.goods_state) sql += ` and goods_state = ${data.goods_state} `;
    if (data.delete_time) sql += ` and delete_time = ${data.delete_time} `;
    sql += ` order by goods_id desc `;
    sql += ` limit ${limit}, ${data.pagesize} `;
    return db.query(sql);
  }
  getCategoryCount(data) {
    let sql = ` select count(*) count from ( select * from goods where 1 `;
    if (data.goods_name) sql += ` and goods_name like '%${data.goods_name}%' `;
    if (data.cat_one_id) sql += ` and cat_one_id like '%${data.cat_one_id}%' `;
    sql += ` group by cat_one_id ) temptable `;
    return db.query(sql);
  }
  findGoodsCategoryList(data) {
    let limit = data.pagesize * (data.page - 1);
    let sql = ` select cat_one_id from goods where 1 `;
    if (data.goods_name) sql += ` and goods_name like '%${data.goods_name}%' `;
    if (data.cat_one_id) sql += ` and cat_one_id like '%${data.cat_one_id}%' `;
    sql += ` group by cat_one_id `;
    sql += ` limit ${limit}, ${data.pagesize} `;
    return db.query(sql);
  }
  update(data, goods_id) {
    return db.query("update goods set ? where goods_id = ?", [data, goods_id]);
  }
  delById(goods_id){
    return db.query("delete from goods where goods_id = ?", [goods_id]);
  }
}

module.exports = new GoodsModel();